﻿Imports TransportadoraToledoDAL
Public Class MarcasBLL
    Public Sub New()

    End Sub
    Dim objDAL As MarcasDAL
#Region "Propriedades"
    Private mar_codigo As Integer
    Public Property pMar_codigo() As Integer
        Get
            Return mar_codigo
        End Get
        Set(ByVal value As Integer)
            mar_codigo = value
        End Set
    End Property
    Private mar_nome As String
    Public Property pMar_nome() As String
        Get
            Return mar_nome
        End Get
        Set(ByVal value As String)
            mar_nome = value
        End Set
    End Property
#End Region
#Region "Métodos"
    Public Function inserir() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mar_nome = "") Then
                Throw New Exception("Informe o nome da marca")
            Else
                objDAL = New MarcasDAL
                objDAL.inserir(Me.mar_nome)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function alterar() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mar_nome = "") Then
                Throw New Exception("Informe o nome da marca")
            ElseIf (Me.mar_codigo = 0) Then
                Throw New Exception("Código inválido")
            Else
                objDAL = New MarcasDAL
                objDAL.alterar(Me.mar_nome, Me.mar_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Public Function excluir() As Boolean
        Dim ret As Boolean = False
        Try
            If (Me.mar_codigo = 0) Then
                Throw New Exception("Código inválido")
            Else
                objDAL = New MarcasDAL
                objDAL.excluir(Me.mar_codigo)
                ret = True
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return ret
    End Function
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New MarcasDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count = 1) Then
                Me.mar_codigo = codigo
                Me.mar_nome = tab.Rows(0)("mar_nome").ToString()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Function localizar(ByVal nome As String) As DataTable
        Dim tab As DataTable = Nothing
        Try

            objDAL = New MarcasDAL
            tab = objDAL.localizar(nome)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function


    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (nome = "ultimo") Then
                objDAL = New MarcasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("mar_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("mar_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New MarcasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("mar_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("mar_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New MarcasDAL
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("mar_codigo").ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function
#End Region
End Class
